package com.authine.cloudpivot.web.api.secondopen.controller;

import com.authine.cloudpivot.web.api.controller.base.BaseController;
import com.authine.cloudpivot.web.api.secondopen.dao.UserMapper;
import com.authine.cloudpivot.web.api.secondopen.domain.HOrgUser;
import com.authine.cloudpivot.web.api.secondopen.modules.ResolveTokenVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.SignatureException;
import io.jsonwebtoken.UnsupportedJwtException;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.bind.DatatypeConverter;

/**
 * @Author: brookpeng
 * @Date: 2021/8/30 21:55
 */
@Api(value = "二开接口", tags = "单点登录解析token")
@RestController
@RequestMapping("/public/resolveToken")
public class OssTokenResolverController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(OssTokenResolverController.class);

    private static final String SECRET_KEY = "HxSecretKey184308302021#!";

    @Autowired
    private UserMapper userMapper;

    @PostMapping(value = "/decipherToken")
    public ResolveTokenVo decipherToken(@RequestParam(name = "token") String token) {
        ResolveTokenVo resolveTokenVo = new ResolveTokenVo();
        Claims body = null;
        try {
            body = Jwts.parser().setSigningKey(DatatypeConverter.parseBase64Binary(SECRET_KEY)).parseClaimsJws(token).getBody();
        } catch (ExpiredJwtException e) {
            e.printStackTrace();
            resolveTokenVo.setCode("1");
            resolveTokenVo.setMsg(e.getMessage() + "获取用户信息失败,token失效");
            return resolveTokenVo;
        } catch (UnsupportedJwtException e) {
            e.printStackTrace();
            resolveTokenVo.setCode("1");
            resolveTokenVo.setMsg(e.getMessage() + "获取用户信息失败");
            return resolveTokenVo;
        } catch (MalformedJwtException e) {
            e.printStackTrace();
            resolveTokenVo.setCode("1");
            resolveTokenVo.setMsg(e.getMessage() + "获取用户信息失败");
            return resolveTokenVo;
        } catch (SignatureException e) {
            e.printStackTrace();
            resolveTokenVo.setCode("1");
            resolveTokenVo.setMsg(e.getMessage() + "获取用户信息失败");
            return resolveTokenVo;
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            resolveTokenVo.setCode("1");
            resolveTokenVo.setMsg(e.getMessage() + "获取用户信息失败");
            return resolveTokenVo;
        }
        String userId = (String) body.get("userId");
        HOrgUser hOrgUser = userMapper.selectUserInfo(userId);
        if (hOrgUser != null) {
            resolveTokenVo.setCode("0");
            resolveTokenVo.setMsg("获取用户信息成功");
            resolveTokenVo.setUid(hOrgUser.getEmployeeNo());
            resolveTokenVo.setUname(hOrgUser.getName());
        } else {
            resolveTokenVo.setCode("1");
            resolveTokenVo.setMsg("获取用户信息失败,未获取到用户信息");
        }
        return resolveTokenVo;
    }
}